package com.fancy.learncenter.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.InviteAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.InviteDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.fancy.learncenter.view.PromptPopupWindow;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyReleaseZpActivity extends BaseActivity {

    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;
    @Bind(R.id.prompt_img)
    ImageView promptImg;


    PromptPopupWindow promptPopupWindow;


    int position = 0;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_release_zp, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);

        initToolbar("我发布的职位");


        xRefreshView.setPullLoadEnable(true);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);
        adapter = new InviteAdapter(this, new ArrayList<InviteDataBean>());
        adapter.setOnclickCallBack(new InviteAdapter.OnclickCallBack() {
            @Override
            public void delete(int item) {

                position = item;
                recruitId = adapter.itemDatas.get(position).getId();

                if (promptPopupWindow == null) {
                    promptPopupWindow = new PromptPopupWindow(MyReleaseZpActivity.this, toolbar);
                    promptPopupWindow.setCallBack(new PromptPopupWindow.CallBack() {
                        @Override
                        public void confirm() {
                            delInvite();
                        }

                        @Override
                        public void cancle() {

                        }
                    });
                }

                promptPopupWindow.showPopupWindow();


            }
        });

        recycleView.setAdapter(adapter);

        adapter.setCustomLoadMoreView(new CustomFootView(this));

        xRefreshView.setPullLoadEnable(true);

        xRefreshView.setCustomHeaderView(new CustomHeaderView(this));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                LogUtil.MyLog("onLoadMore", "====onRefresh==");
                getDatas(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
                getDatas(false);
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });
        getDatas(true);
    }

    InviteAdapter adapter;


    private int page = 0;

    private void getDatas(final boolean isRefresh) {
        Map map = new HashMap();
        map.put("token", SPUtils.getToken());
        map.put("userId", SPUtils.getUserID());
        if (isRefresh) {
            page = 0;
        } else {
            page++;
        }
        map.put("page", page + "");
        HttpMehtod.getInstance().inviteList(map, new IdeaObserver<BaseDataBean<List<InviteDataBean>>>(this, true) {

            @Override
            public void onSuccess(BaseDataBean<List<InviteDataBean>> listBaseDataBean) {
//
                if (isRefresh) {
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);
                    if (listBaseDataBean.getData() == null) {
                        adapter.notifyDataSetChanged(new ArrayList<InviteDataBean>());
                        promptImg.setVisibility(View.VISIBLE);
                    } else {
                        promptImg.setVisibility(View.GONE);
                        adapter.notifyDataSetChanged((ArrayList<InviteDataBean>) listBaseDataBean.getData());
                    }

                } else {
                    if (listBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }


                    if (listBaseDataBean.getData() == null) {
                        return;
                    }
                    adapter.addAll(listBaseDataBean.getData());
                }
            }

            @Override
            public void onFail(int errorCode) {
                xRefreshView.stopRefresh();
                xRefreshView.setLoadComplete(false);
            }
        });

    }

    String recruitId = "";

    private void delInvite() {
        Map map = new HashMap();
        map.put("recruitId", recruitId);
        HttpMehtod.getInstance().delInvite(map, new IdeaObserver<BaseDataBean>(this, true) {

            @Override
            public void onSuccess(BaseDataBean baseDataBean) {
//                getDatas(true);
                adapter.itemDatas.remove(position);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }
}
